﻿using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nMongo
{
    public partial class MongoEngine
    {
        public virtual bool DeleteById<T>(object id) 
            where T : class
        {
            WriteConcernResult result;
            return DeleteById<T>(id, out result);
        }

        public virtual bool DeleteById<T>(object id, out WriteConcernResult result) 
            where T : class
        {
            return DeleteById<T>(id, WriteConcern.Acknowledged, out result);
        }

        public virtual bool DeleteById<T>(object id, WriteConcern concern, out WriteConcernResult result, T deletedObject = null) 
            where T : class
        {
            if (deletedObject == null)
                deletedObject = GetById<T>(id);

            result = this.Database.GetCollectionFromType<T>().Remove(this.BuildIdentityQuery(id), concern);
            bool returned = (result != null && result.Ok || result.DocumentsAffected > 0);

            //if (returned)
            //{
            //    this.AutoDetectPersistencyById(MongoPersistencyState.Removed, id);
            //    this.AutoDetectGraveyardAdd(deletedObject);
            //}
            return returned;
        }
    }
}
